#define _CRT_SECURE_NO_WARNINGS
#include"Stack.h"

bool ADD_capacity(ST* ps)
{
	assert(ps);
	STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(STDataType) * (ps->capacity) * 2);
	if (tmp == NULL)
		return false;
	ps->capacity = ps->capacity * 2;
	ps->a = tmp;
	return true;
}

void StackInit(ST* ps)
{
	assert(ps);
	STDataType* tmp = (STDataType*)malloc(sizeof(STDataType)*8);
	if (tmp == NULL)
	{
		printf("%s", strerror(errno));
		return;
	}
	ps->a = tmp;
	ps->capacity = 8;
	ps->top = 0;
}
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		assert(ADD_capacity(ps));
	}
	ps->a[ps->top] = x;
	ps->top++;

}
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top>0);
	ps->top--;
}
STDataType StackTOP(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top-1];
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	if (ps->top == 0)
		return true;
	else
		return false;
}